package cn.iocoder.yudao.module.ym.controller.admin.connection;

import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.ym.controller.admin.connection.vo.*;
import cn.iocoder.yudao.module.ym.dal.dataobject.connection.ConnectionDO;
import cn.iocoder.yudao.module.ym.service.connection.ConnectionService;

@Tag(name = "管理后台 - 区域电站人员关联")
@RestController
@RequestMapping("/ym/connection")
@Validated
public class ConnectionController {

    @Resource
    private ConnectionService connectionService;

    @PostMapping("/create")
    @Operation(summary = "创建区域电站人员关联")
    public CommonResult<Long> createConnection(@Valid @RequestBody ConnectionSaveReqVO createReqVO) {
        return success(connectionService.createConnection(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新区域电站人员关联")
    public CommonResult<Boolean> updateConnection(@Valid @RequestBody ConnectionSaveReqVO updateReqVO) {
        connectionService.updateConnection(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除区域电站人员关联")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteConnection(@RequestParam("id") Long id) {
        connectionService.deleteConnection(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得区域电站人员关联")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<ConnectionRespVO> getConnection(@RequestParam("id") Long id) {
        ConnectionDO connection = connectionService.getConnection(id);
        return success(BeanUtils.toBean(connection, ConnectionRespVO.class));
    }

    @GetMapping("/getPersonList")
    @Operation(summary = "获得当前用户绑定的区域下的所有人")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<List<AdminUserDO>> getPersonList() {
        return success( connectionService.getPersonList());
    }

    @GetMapping("/page")
    @Operation(summary = "获得区域电站人员关联分页")
    public CommonResult<PageResult<ConnectionRespVO>> getConnectionPage(@Valid ConnectionPageReqVO pageReqVO) {
        return success(connectionService.getConnectionPage(pageReqVO));
    }

    @GetMapping("/list")
    @Operation(summary = "获得区域电站人员关联分页")
    public CommonResult<List<ConnectionRespVO>> getConnectionList(@Valid ConnectionPageReqVO pageReqVO) {
        return success(connectionService.getConnectionList(pageReqVO));
    }




    @GetMapping("/export-excel")
    @Operation(summary = "导出区域电站人员关联 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportConnectionExcel(@Valid ConnectionPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        // 导出 Excel
        ExcelUtils.write(response, "区域电站人员关联.xls", "数据", ConnectionRespVO.class,
                connectionService.getConnectionPage(pageReqVO).getList());
    }

}
